import { createRouter, createWebHistory, RouteRecordRaw } from 'vue-router'
import { useUserStore, useMenuStore } from '@/plugins/store'
import Home from '@/views/home/Home.vue'
import PageLoading from '@/views/page-loading/PageLoading.vue'
import Page404 from '@/views/page404/Page404.vue'
// import PageMicro from '@/views/page-micro/PageMicro.vue'
import PageLogin from '@/views/page-login/PageLogin.vue'
import PageWip from '@/views/page-wip/PageWip.vue'
import PageUser from '@/views/page-user/PageUser.vue'

export const Page404Route = {
  path: '/:path*',
  name: 'Page404',
  component: Page404
}

export const PageLoadingRoute = {
  path: '/:path*',
  name: 'PageLoading',
  component: PageLoading
}

const routes: RouteRecordRaw[] = [
  {
    path: '/login',
    name: 'Login',
    component: PageLogin,
    meta: {
      isFullScreen: true
    }
  },
  {
    path: '/',
    name: 'Home',
    component: Home
  },
  // {
  //   path: '/micro',
  //   name: 'Micro',
  //   component: PageMicro
  // },
  {
    path: '/wip',
    name: 'Wip',
    component: PageWip
  },
  {
    path: '/user',
    name: 'User',
    component: PageUser
  },
  PageLoadingRoute
]

const router = createRouter({
  history: createWebHistory('/ykf'),
  routes
})

router.beforeEach((to) => {
  const user = useUserStore()
  if (!user.token && to.name !== 'Login') return { name: 'Login' }
  // 页面加入到tag上面
  const menus = useMenuStore()
  menus.addRoute(to)
})

export default router
